package org.abettor.pushbox.download;

import java.util.HashMap;
import java.util.Map;

import org.abettor.pushbox.config.Config;
import org.abettor.pushbox.db.PushBoxMapBeanManage;
import org.abettor.pushbox.model.UserInfoBean;
import org.abettor.pushbox.net.NetTools;
import org.abettor.pushbox.R;

import android.app.Activity;
import android.app.ProgressDialog;
import android.os.Handler;

/**
 * 读取用户信息的操作
 * 
 * @author jzh
 * 
 */
public class DownloadUserInfo {
	// 更新界面信息
	public static interface UpadateView {
		// 更新的操作
		public void update(UserInfoBean userInfo);
	}

	private UpadateView update;
	private Handler mHandler;
	private Activity context;
	private ProgressDialog progress;// 进度条对话框
	private NetTools netTools = new NetTools();

	public DownloadUserInfo(Activity context, UpadateView update) {
		this.context = context;
		mHandler = new Handler();
		this.update = update;
	}
	/**
	 * 读取用户消息
	 * @param nickName
	 * @return
	 */
	public String readUserStr(String nickName){
		Map<String,String> map = new HashMap<String,String>();
		map.put("nickName", nickName);
		String ret = netTools.postData(Config.queryUserInfo, map);
		return ret;
	}
	/**
	 * 读取用户的信息
	 * 
	 * @return
	 */
	public UserInfoBean readUserInfo(final String nickName) {
		progress = new ProgressDialog(context);
		progress.setTitle(R.string.mod_setting);
		progress.setMessage(context
				.getText(R.string.option_activity_info_geting));
		progress.show();
		final UserInfoBean info = new UserInfoBean();
		info.setNickName(nickName);
		Thread thread = new Thread() {
			@Override
			public void run() {
				PushBoxMapBeanManage bean = new PushBoxMapBeanManage(context);
				int selfCount = bean.getMyCreateMapCount();
				int resovleCoutn = bean.getClassicPassCount();
				info.setMyPassCount(String.valueOf(resovleCoutn));
				info.setMyCreateCount(String.valueOf(selfCount));
				// 读取网络消息
				Map<String, String> map = new HashMap<String, String>();
				map.put("nickName", nickName);
				String ret = netTools.postData(Config.queryUserInfo, map);
				progress.dismiss();
				if (ret == null) {
					mHandler.post(new Runnable() {
						@Override
						public void run() {
							update.update(info);
						}
					});
					return;
				}
				ParaDownloadRetValue.paraUserInfo(ret, info);
				mHandler.post(new Runnable() {
					@Override
					public void run() {
						update.update(info);
					}
				});
			}
		};
		thread.start();
		return info;
	}
}
